package reflect;

import java.io.*;

/**
 * 静态内部类方式
 * JVM保证单例
 * 加载外部类时不会加载内部类，这样可以实现懒加载
 */
//不能通过反射和序列化测试
public class Mgr07 implements Serializable {
    long serialVersionUID = 1L;

    private Mgr07() {
    }

    private static class Mgr07Holder {
        private final static Mgr07 INSTANCE = new Mgr07();
    }

    public static Mgr07 getInstance() {
        return Mgr07Holder.INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
//        for(int i=0; i<100; i++) {
//            new Thread(()->{
//                System.out.println(Mgr07.getInstance().hashCode());
//            }).start();
//        }


        try {
            Mgr07 s = Mgr07.getInstance();
            Mgr07 s2 = null;
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Mgr07.txt"));
            out.writeObject(s);
            s2 =(Mgr07) new ObjectInputStream(new FileInputStream("Mgr07.txt")).readObject();
            System.out.println(s2 == s); //false
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


}
